package com.ehotting.edsta.circuitDesign.gen;

import com.alibaba.fastjson.JSON;
import com.ehotting.edsta.circuitDesign.enums.DataTypeEnum;
import com.ehotting.edsta.circuitDesign.domain.EsCircuitCodeData;
import com.ehotting.edsta.circuitDesign.domain.EsElement;
import com.ehotting.edsta.circuitDesign.gen.element.helper.NumberHelper;

import java.util.Map;

public class DataTypeConvertHelper {

    public static Object convertValue(EsElement esElement){

        if(esElement.getDataType() == DataTypeEnum.INTEGER){
            return NumberHelper.parseInteger(esElement.getValue());
        }else if(esElement.getDataType() == DataTypeEnum.LONG){
            return NumberHelper.parseLong(esElement.getValue());
        }else if(esElement.getDataType() == DataTypeEnum.FLOAT){
            return NumberHelper.parseFloat(esElement.getValue());
        }else if(esElement.getDataType() == DataTypeEnum.DOUBLE){
            return NumberHelper.parseDouble(esElement.getValue());
        }else if(esElement.getDataType() == DataTypeEnum.BOOLEAN){
            return Boolean.parseBoolean(esElement.getValue());
        }else if(esElement.getDataType() == DataTypeEnum.OBJECT){
            return esElement.getValue();
        }else {
            return esElement.getValue();
        }
    }


    public static Object convertParamValue(EsCircuitCodeData circuitCodeData){
        if(circuitCodeData.getCircuitCodeDataTemplate().getDataType() == DataTypeEnum.INTEGER){
            return NumberHelper.parseInteger(circuitCodeData.getValue());
        }else if(circuitCodeData.getCircuitCodeDataTemplate().getDataType() == DataTypeEnum.LONG){
            return NumberHelper.parseLong(circuitCodeData.getValue());
        }else if(circuitCodeData.getCircuitCodeDataTemplate().getDataType() == DataTypeEnum.FLOAT){
            return NumberHelper.parseFloat(circuitCodeData.getValue());
        }else if(circuitCodeData.getCircuitCodeDataTemplate().getDataType() == DataTypeEnum.DOUBLE){
            return NumberHelper.parseDouble(circuitCodeData.getValue());
        }else if(circuitCodeData.getCircuitCodeDataTemplate().getDataType() == DataTypeEnum.BOOLEAN){
            return Boolean.parseBoolean(circuitCodeData.getValue());
        }else if(circuitCodeData.getCircuitCodeDataTemplate().getDataType() == DataTypeEnum.OBJECT){
            return JSON.parseObject(circuitCodeData.getValue(), Map.class);
        }else {
            return circuitCodeData.getValue();
        }
    }

}
